<%@ page import="gz.gae.blog.impl.BlogManagerImpl" %>
<%@ page import="java.util.List" %>
<%@ page import="com.google.appengine.api.users.User" %>
<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>
<%@ page import="com.google.appengine.api.users.UserService" %>
<%@ page import="gz.gae.blog.ioc.WebBlogUtil" %>
<%@ page import="gz.gae.blog.BlogManager" %>
<%@ page import="gz.gae.blog.entity.Blog" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="/WEB-INF/jsp/_taglibs.jsp" %>
<%
    UserService userService = UserServiceFactory.getUserService();
    User user = userService.getCurrentUser();
    request.setAttribute("user", user);
    request.setAttribute("userService", userService);
%>
<%
    BlogManager bm = WebBlogUtil.getBlogManager();
    List<Blog> blogs;
    String s = request.getParameter("month");
    if (s != null) {
        blogs = bm.getAllByMonth(Integer.parseInt(s));
    } else {
        blogs = bm.getAll();
    }
    request.setAttribute("blogs", blogs);
%>
<script type="text/javascript">
    function send_mail(bId)
    {
        var callback = function()
        {
            alert("ok");
        }
        $.get("admin/send_blog_to_mail.action", {bId:bId}, callback);
    }
    function confirmDelete()
    {
        return  window.confirm("Do you really want to delete it?")
    }
</script>
<c:if test="${empty blogs}">
    No content
</c:if>
<c:forEach items="${blogs}" var="blog">
    <div class="post type-post hentry">

        <h2 class="entry-title">
            <a href="main.action?pageType=view&bId=${blog.id}" rel="bookmark">${blog.subject}</a>
        </h2>

        <div class="entry-meta">
            <span class="meta-sep">由</span>
            <span class="author vcard">
                    ${blog.owner.nickname}
            </span>
            <span class="meta-prep meta-prep-author">发表于</span>
            <span class="entry-date"><f:formatDate value="${blog.createDate}" pattern="yyyy-MM-dd hh:mm"/></span>
            <span class="meta-prep meta-prep-author">修改于</span>
            <span class="entry-date"><f:formatDate value="${blog.modifyDate}" pattern="yyyy-MM-dd hh:mm"/></span>

        </div>
        <!-- .entry-meta -->

        <div class="entry-content">
                ${blog.content}
        </div>
        <!-- .entry-content -->

        <div class="entry-utility">
            <c:if test="${userService.userLoggedIn&&userService.userAdmin}">
                <span class="comments-link">
                    <a href="admin/delete_blog.action?bId=${blog.id}" onclick="return confirmDelete()">删除</a>
                </span>
                <span class="meta-sep">|</span>
                <span class="comments-link">
                    <a href="admin/update_blog_view.action?bId=${blog.id}">编辑</a>
                </span>
                <span class="meta-sep">|</span>
                <span class="comments-link">
                    <a href="javascript:send_mail(${blog.id})">发送到邮箱</a>
                </span>
            </c:if>
        </div>
        <!-- .entry-utility -->
    </div>
</c:forEach>
